/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.dkasza.qicky;

import java.io.*;


/**
 *
 * @author Dániel
 */
public class Xml {
    
    public static class Template {

        public Template(String text) {
            template = text;
        }
        
        public static Template fromFile(String path) {
            return new Template(fileToString(path));
        }
        
        public void subs(String key, String value) {
            template = template.replace("<&"+key+'>', value);
        }
        
        public String render(String replace) {
            return template.replaceAll("<&[^>]*>", replace);
        }
        
        private String template;
        
    }
    
    public static String fileToString(String path) {
        try(FileInputStream in = new FileInputStream(path)){
            File file = new File(path);
            byte[] buff = new byte[(int)file.length()];
            in.read(buff);
            return new String(buff);
        } catch(IOException ioe) {
            return "File Not Found: " + path;
        }
    }
    
    public static String escape(String in) {
        in = in.replace("&", "&amp;")
               .replace("\"", "&quot;")
               .replace("<", "&lt;")
               .replace(">", "&gt;")
               .replace("\n", "<br />");
        return in;
    }
}
